﻿using System.Windows.Input;
using Infrastructure.BaseClasses;
using Microsoft.Practices.Prism.Commands;
using MVVMBasic.Interfaces;
using Infrastructure.Interfaces;
using MVVMBasicRIALibrary.Web.DataModel;

namespace MVVMBasic.ViewModel
{
    public class CustomerDetailsViewViewModel : ViewModelBase, ICustomerDetailsViewViewModel
    {
        private bool _isNew;
        private IRepository<Dictionary> _repository;

        public CustomerDetailsViewViewModel()
        {
                
        }
        
        #region Properties
        public Dictionary WorkingItem { get; private set; }
        #endregion

        #region ICommands
        private ICommand _saveCommand;
        private ICommand _cancelCommand;

        public ICommand SaveCommand
        {
            get { return _saveCommand ?? (_saveCommand = new DelegateCommand<object>(OnSaveCommand)); }
        }
        public ICommand CancelCommand
        {
            get { return _cancelCommand ?? (_cancelCommand = new DelegateCommand<object>(OnCancelCommand)); }
        }
        #endregion

        #region Methods
        public void Initialize(Dictionary obj, IRepository<Dictionary> repository)
        {
            _repository = repository;

            if (obj.Id == 0)
                _isNew = true;

            WorkingItem = obj;
        }
        private void OnSaveCommand(object obj)
        {
            if (_isNew)
            {
                _repository.Add(WorkingItem);
            }
            _repository.SaveChanges();
            Close();
        }
        private void OnCancelCommand(object obj)
        {
            Close();
        }
        #endregion
    }
}